﻿
Imports System.Runtime.CompilerServices
Imports osi.root.lock
Imports osi.root.lock.slimlock
Imports osi.root.delegates

Public Module _lock
    <Extension()> Public Sub reader_locked(ByRef this As duallock, ByVal d As void)
        k_reader_locked(this, Sub() void_(d))
    End Sub

    <Extension()> Public Function reader_locked(Of T)(ByRef this As duallock,
                                                      ByVal d As _do(Of T),
                                                      Optional ByVal false_value As T = Nothing) As T
        Return k_reader_locked(this, Function() do_(d, false_value))
    End Function

    <Extension()> Public Sub writer_locked(ByRef this As duallock, ByVal d As void)
        k_writer_locked(this, Sub() void_(d))
    End Sub

    <Extension()> Public Function writer_locked(Of T)(ByRef this As duallock,
                                                      ByVal d As _do(Of T),
                                                      Optional ByVal false_value As T = Nothing) As T
        Return k_writer_locked(this, Function() do_(d, false_value))
    End Function

    <Extension()> Public Sub locked(Of T As islimlock)(ByRef this As T, ByVal d As void)
        k_locked(this, Sub() void_(d))
    End Sub

    <Extension()> Public Function locked(Of T As islimlock, OT) _
                                        (ByRef this As T,
                                         ByVal d As _do(Of OT),
                                         Optional ByVal false_value As OT = Nothing) As OT
        Return k_locked(this, Function() do_(d, false_value))
    End Function

    <Extension()> Public Sub reenterable_locked(Of T As ilock) _
                                               (ByRef this As T, ByVal d As void)
        k_reenterable_locked(this, Sub() void_(d))
    End Sub

    <Extension()> Public Function reenterable_locked(Of T As ilock, OT) _
                                                    (ByRef this As T,
                                                     ByVal d As _do(Of OT),
                                                     Optional ByVal false_value As OT = Nothing) As OT
        Return k_reenterable_locked(this, Function() do_(d, false_value))
    End Function

    <Extension()> Public Sub reenterable_locked(ByRef this As slimlock.monitorlock, ByVal d As void)
        k_reenterable_locked(this, Sub() void_(d))
    End Sub

    <Extension()> Public Function reenterable_locked(Of T)(ByRef this As slimlock.monitorlock,
                                                           ByVal d As _do(Of T),
                                                           Optional ByVal false_value As T = Nothing) As T
        k_reenterable_locked(this, Function() do_(d, false_value))
    End Function

    <Extension()> Public Sub reenterable_locked(ByRef this As lock.monitorlock, ByVal d As void)
        k_reenterable_locked(this, Sub() void_(d))
    End Sub

    <Extension()> Public Function reenterable_locked(Of T)(ByRef this As lock.monitorlock,
                                                           ByVal d As _do(Of T),
                                                           Optional ByVal false_value As T = Nothing) As T
        k_reenterable_locked(this, Function() do_(d, false_value))
    End Function

    Public Sub assert_in_lock(Of T As ilock)(ByRef l As T)
        assert(l.held_in_thread())
    End Sub
End Module
